
import { _decorator, Component, Node, SkeletalAnimation, Collider, ITriggerEvent, ICollisionEvent, Animation } from 'cc';
import { GameContrl } from './GameContrl';
import { ViewMgr } from './Mgr/ViewMgr';
import { Player } from './player';
import { GameView } from './ui/GameView';
const { ccclass, property } = _decorator;

@ccclass('Cat')
export class Cat extends Component {

    @property(SkeletalAnimation) animationCom: SkeletalAnimation = null!;

    private moveDir: number = 0;

    // @property(Collider) collider: Collider = null!;

    start() {
        // [3]
        this.animationCom.play('run');
        let collider = this.getComponent(Collider) as Collider;
        collider.on('onTriggerEnter', this.onTriggerEnter, this);
    }

    setDirction(dir: number) {
        this.moveDir = dir;
        //角色
        this.node.setPosition(this.node.getPosition().add3f(1.25 * this.moveDir, 0, 0))
        if (this.node.getPosition().x >= 2) this.node.setPosition(2, 0, 0)
        if (this.node.getPosition().x <= - 2) this.node.setPosition(-2, 0, 0)

    }

    onTriggerEnter(event: ITriggerEvent) {

        if (event.otherCollider.node.name == 'Fishbone') {
            event.otherCollider.node.active = false;
            GameContrl.instance.eatFish();
        }
        if (event.otherCollider.node.name == 'obstacle01') {
            event.otherCollider.node.getComponent(Animation)?.play('ObstacleDeath')
            event.otherCollider.enabled = false;
            this.fail();
        }

    }

    fail() {
        this.animationCom.play('death');
        Player.instance.speed = 0;
        Player.instance.isDeath = true;
        console.log(' ViewMgr.getInstance()', ViewMgr)
        ViewMgr.getInstance().openOver(this.node.scene)
    }

}
